package cn.com.lms.business.cozmgt.service;

import cn.com.lms.business.course.persistence.entity.CourseCategoryTree;
import cn.com.lms.business.cozmgt.persistence.entity.CatalogManage;
import cn.com.lms.business.cozmgt.persistence.repository.CatalogManageRepository;


import com.bnzj.core.persistence.BaseService;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 16266
 * @DateTime: 2020/3/13 14:17
 * @Description: 目录管理业务层
 */
@Service
public class CatalogManageService extends BaseService<CatalogManageRepository, CatalogManage, Long> {

    /**
     * 查询条件构造
     *
     * @return
     */
    public QueryHelpBean getQueryHelpBean() {
        String selectSQL = "select  d.* ";
        String whereSQL = " from bnz_catalog_manage d where 1=1 ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, CatalogManage.class);
        return queryBean;
    }


    /**
     * 查询课程目录树结构
     *
     * @return
     */
    public List<CourseCategoryTree> getCategoryTreeList(HttpServletRequest request) {
        List<Map> objects = this.repository.queryCategory();
        List<CourseCategoryTree> parentDeptList = new ArrayList<>();
        List<CourseCategoryTree> childDeptList = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        objects.forEach(item -> {
            CourseCategoryTree sysDept = objectMapper.convertValue(item, CourseCategoryTree.class);
            if (0L == sysDept.getParentId()) {
                parentDeptList.add(sysDept);
            } else {
                childDeptList.add(sysDept);
            }
        });
        return getTreeList(parentDeptList, childDeptList);
    }


    private List<CourseCategoryTree> getTreeList(List<CourseCategoryTree> parentList, List<CourseCategoryTree> childList) {
        //声明一个map，用来过滤已操作过的数据
        Map<Long, Long> map = Maps.newHashMapWithExpectedSize(childList.size());
        parentList.forEach(item -> getChild(item, map, childList));
        return parentList;
    }

    private void getChild(CourseCategoryTree dept, Map<Long, Long> map, List<CourseCategoryTree> bodyList) {
        List<CourseCategoryTree> childList = Lists.newArrayList();
        bodyList.stream()
                .filter(c -> !map.containsKey(c.getId()))
                .filter(c -> c.getParentId() == dept.getId())
                .forEach(c -> {
                    map.put(c.getId(), c.getParentId());
                    getChild(c, map, bodyList);
                    childList.add(c);
                });
        dept.setChildList(childList);//子数据集
    }


    @EntityGraph(value = "CatalogManage.Graph", type = EntityGraph.EntityGraphType.FETCH)
    public Page<CatalogManage> findAllCatalogManage(QueryHelpBean bean) {
        return queryService.findAll(bean);
    }
}
